// $codepro.audit.disable
package edu.gatech.oad.cyberdoc.view;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.SystemColor;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.AbstractListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.LineBorder;

import net.miginfocom.swing.MigLayout;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import edu.gatech.oad.cyberdoc.presenter.Presenter;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AppointmentGroupsPanel extends ApplicationPanel {
	/**
	 * This class's logger.
	 */
	private static final Logger LOG;

	private JTextField textField;

	private JList list;

	private JComboBox doctorsBox;

	private long[] times;
	private String[] doctors;
	private JTextPane textPane;

	static {
		LOG = Logger
				.getLogger("edu.gatech.oad.cyberdoc.view.AppointmentGroupsPanel");
	}

	/**
	 * Create the panel.
	 */
	public AppointmentGroupsPanel() {
		setBackground(SystemColor.inactiveCaptionBorder);
		setLayout(null);

		JPanel topHalf = new JPanel();
		topHalf.setBackground(SystemColor.inactiveCaptionBorder);
		topHalf.setBounds(0, 0, 584, 195);
		add(topHalf);
		topHalf.setLayout(null);

		JPanel toolbar = new JPanel();
		toolbar.setBackground(SystemColor.inactiveCaptionBorder);
		FlowLayout fl_toolbar = (FlowLayout) toolbar.getLayout();
		fl_toolbar.setAlignment(FlowLayout.LEFT);
		toolbar.setBounds(10, 11, 564, 33);
		topHalf.add(toolbar);

		JLabel lblPleaseSelectA = new JLabel("Please select a date and time. ");
		toolbar.add(lblPleaseSelectA);

		JScrollPane tablePanel = new JScrollPane();
		tablePanel.setBounds(10, 48, 564, 136);
		topHalf.add(tablePanel);

		list = new JList();
		list.setModel(new AbstractListModel() {
			String[] values = new String[] {};

			public int getSize() {
				return values.length;
			}

			public Object getElementAt(int index) {
				return values[index];
			}
		});
		tablePanel.setViewportView(list);

		JPanel bottomHalf = new JPanel();
		bottomHalf.setBorder(new LineBorder(new Color(0, 0, 0)));
		bottomHalf.setBackground(SystemColor.menu);
		bottomHalf.setBounds(10, 218, 564, 163);
		add(bottomHalf);
		bottomHalf.setLayout(new MigLayout("", "[][grow]", "[][][][grow][][]"));

		JLabel lblName = new JLabel("Name: ");
		bottomHalf.add(lblName, "cell 0 0,alignx trailing");

		textField = new JTextField();
		bottomHalf.add(textField, "cell 1 0,growx");
		textField.setColumns(10);

		JLabel lblEmail = new JLabel("Doctor:");
		bottomHalf.add(lblEmail, "cell 0 1,alignx trailing");

		doctorsBox = new JComboBox();
		bottomHalf.add(doctorsBox, "cell 1 1,growx");

		JLabel lblContact = new JLabel("Reason:");
		bottomHalf.add(lblContact, "cell 0 2,alignx trailing");

		textPane = new JTextPane();
		bottomHalf.add(textPane, "cell 1 2 1 3,grow");

		JButton submitButton = new JButton("Submit");
		submitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int index = list.getSelectedIndex();

				if (index != -1) {
					Presenter.getInstance().selectAppointment(times[index],
							doctors[doctorsBox.getSelectedIndex()],
							textPane.getText());
				} else if (times.length > 0) {
					JOptionPane.showMessageDialog(null,
							"You must select the time of the appointment"
									+ " you wish to reserve.",
							"Select Appointment Time",
							JOptionPane.ERROR_MESSAGE);
				} else {
					JOptionPane.showMessageDialog(null,
							"There are no appointments available for this day",
							"No Appointments Available",
							JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		bottomHalf.add(submitButton, "cell 1 5");

		JLabel lblPleaseSelctA = new JLabel(
				"Please select a doctor, and add the reason for your appointment.");
		lblPleaseSelctA.setBounds(10, 193, 366, 14);
		add(lblPleaseSelctA);

	}

	@Override
	public void show(JSONObject response) {
		if (response.has("times")) {
			try {
				JSONArray times = response.getJSONArray("times");
				Object[] data = new Object[times.length()];
				this.times = new long[data.length];

				for (int i = 0; i < times.length(); i++) {
					// TODO Put this into a more readable format
					long time = Long.parseLong(times.get(i).toString());
					data[i] = new Date(time).toString();
					this.times[i] = time;
				}

				list.setListData(data);
			} catch (JSONException e) {
				LOG.log(Level.SEVERE, "", e);
			}
		}

		if (response.has("doctors")) {
			try {
				JSONArray doctors = response.getJSONArray("doctors");
				this.doctors = new String[doctors.length()];

				for (int i = 0; i < doctors.length(); i++) {
					String username = ((JSONObject) doctors.get(i)).get(
							"username").toString();
					doctorsBox.addItem(username);
					this.doctors[i] = username;
				}

			} catch (JSONException e) {
				LOG.log(Level.SEVERE, "", e);
			}
		}

		if (response.has("username")) {
			try {
				textField.setText(response.getString("username"));
			} catch (JSONException e) {
				LOG.log(Level.SEVERE, "", e);
			}
		}
	}
}
